﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.BizFacade
{
    using System;
    using System.Data;

    using Boiling.Common;
    using Boiling.DataAccess;
    
    
    public class FriendManager
    {

        /// <summary>
        /// 处理好友申请请求
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="guestID"></param>
        /// <returns></returns>
        public bool ApplyFriend(String hostID, String guestID)
        {
            //如果已经是好友，不能再发出申请
            if ( (new b_Friends()).iffriend(hostID, guestID) )
                return false;
            
            //如果已经发出过好友申请，不能再发出申请
            if ((new b_Friends()).iffriend1(hostID, guestID))
                return false;

            return (new b_Friends()).insertFrined(hostID, guestID);
        }

        /// <summary>
        /// 接受好友申请
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="guestID"></param>
        /// <returns></returns>
        public bool AcceptFriendRequest(String hostID, String guestID)
        {
               return (new b_Friends()).confirmFriend(hostID, guestID);
        }

        /// <summary>
        /// 通过用户邮箱返回向该用户申请成为好友但尚未获得通过的UserInfo封装类数组
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns>UserInfo封装类数组</returns>
        public c_UserInfo[] GetApplyedFriendList(String hostID)
        {
            b_Friendsdata friendsdata = new b_Friendsdata();
            friendsdata = (new b_Friends()).getfriendnotbyID(hostID);
            int n = friendsdata.Tables[b_Friendsdata.b_Friends_Table].Rows.Count;
            c_UserInfo[] userinfo = new c_UserInfo[n];
            for (int i = 0; i < n; i++ )
            {
                userinfo[i] = new c_UserInfo();
                String id = friendsdata.Tables[b_Friendsdata.b_Friends_Table].Rows[i][b_Friendsdata.Guest_User_Field].ToString();
                userinfo[i] = (new UserSystem()).GetUserInfodatabyID(id);
            }

            return userinfo;
        }

        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="guestID"></param>
        /// <returns></returns>
        public bool deletFriend(String hostID, String guestID)
        {
            return (new b_Friends()).deleteFriends(hostID, guestID);
        }

        /// <summary>
        /// 创建好友关系网
        /// </summary>
        public void GenerateFriendshipNet()
        {

        }

        /// <summary>
        /// 根据host用户ID查找所有好友
        /// </summary>
        /// <param name="hostID">主用户id</param>
        /// <returns>该用户的好友列表</returns>
        public b_Friendsdata SearchAllFriendsbyUserId(String hostID)
        {
            return (new b_Friends()).getfriendsbyID(hostID);
        }


        /// <summary>
        /// 返回用户所有好友的封装类
        /// </summary>
        /// <param name="hostID">用户id</param>
        /// <returns></returns>
        public c_UserInfo[] GetAllFriendsByUserID(String hostID)
        {
            int n = countfriendbyID(hostID);
            String[] res = new String[n];
            res = SearchAllFriendsbyUserIDReturnString(hostID);
            c_UserInfo[] userinfo = new c_UserInfo[n];
            for (int i = 0; i < n; i++ )
            {
                userinfo[i] = new c_UserInfo();
                userinfo[i] = (new UserSystem()).GetUserInfodatabyID(res[i]);
            }
            return userinfo;
        }



        

        /// <summary>
        /// 根据hostID返回用户的所有好友邮箱
        /// </summary>
        /// <param name="hostID">用户hostID</param>
        /// <returns></returns>
        public String[] SearchAllFriendsbyUserIDReturnString(String hostID)
        {
            int n = (new FriendManager()).countfriendbyID(hostID);
            String[] res = new String[n];
            for (int i = 0; i < n; i++ )
            {
                res[i] = SearchAllFriendsbyUserId(hostID).Tables[b_Friendsdata.b_Friends_Table].Rows[i][b_Friendsdata.Guest_User_Field].ToString();
            }
            return res;
        }





        /// <summary>
        /// 给定用户昵称,返回一个数组,数组内元素为匹配的用户的邮箱
        /// </summary>
        /// <param name="username">用户昵称</param>
        /// <returns>存放邮箱的数组</returns>
        public String[] SearchUserbyUsernameReturnString(String username)
        {
            int n = (new b_UserInfo()).countuserbyname(username);
            String[] result = new String[n];
            b_UserInfodata userInfoData = (new b_UserInfo()).getuserinfobyname(username);
            for (int i = 0; i < n; i++)
            {
                result[i] = userInfoData.Tables[b_UserInfodata.b_UserInfo_Table].Rows[i][b_UserInfodata.User_ID_Field].ToString();
            }

            return result;
        }


        /// <summary>
        /// 根据用户昵称查找用户，返回所有用户的封装类组
        /// </summary>
        /// <param name="username">用户昵称</param>
        /// <returns>查找到的封装类组</returns>
        public c_UserInfo[] GetUserInfoDataByUsernameReturnClass(String username)
        {
            b_UserInfodata userinfodata = new b_UserInfodata();
            userinfodata = (new b_UserInfo()).getuserinfobyname(username);
            int n = userinfodata.Tables[b_UserInfodata.b_UserInfo_Table].Rows.Count;
            if (n == 0)
                return null;
            c_UserInfo[] userinfo = new c_UserInfo[n];
            for (int i = 0; i < n; i++)
            {
                userinfo[i] = new c_UserInfo();
                String id = userinfodata.Tables[b_UserInfodata.b_UserInfo_Table].Rows[i][b_UserInfodata.User_ID_Field].ToString();
                userinfo[i] = (new UserSystem()).GetUserInfodatabyID(id);
            }

            return userinfo;
        }

        /// <summary>
        /// 通过邮箱返回用户昵称
        /// </summary>
        /// <param name="hostID">用户邮箱</param>
        /// <returns>用户昵称</returns>
        public String GetNamebyUserID(String hostID)
        {
            b_UserInfodata dataSet;
            using (DataAccess.b_UserInfo userDataAccess = new DataAccess.b_UserInfo())
            {
                dataSet = userDataAccess.getuserinfobyID(hostID);
            }
            DataRowCollection rows = dataSet.Tables[b_UserInfodata.b_UserInfo_Table].Rows;
            return rows[0][b_UserInfodata.User_Name_Field].ToString();
        }

        
        /// <summary>
        /// 根据用户昵称查找相应的所有用户
        /// </summary>
        /// <param name="username">用户昵称</param>
        /// <returns>包含符合该昵称的所有用户</returns>
        public b_UserInfodata SearchUsersbyUsername(String username)
        {
            return (new b_UserInfo()).getuserinfobyname(username);
        }

        /// <summary>
        /// g根据给点的用户昵称查找符合该昵称的用户数量
        /// </summary>
        /// <param name="username">用户昵称</param>
        /// <returns>匹配的用户数量</returns>
        public int GetUsersCountbyUsername(String username)
        {
            //int n = (new b_UserInfo()).countuserbyname(username);

            // return n;
            return (new b_UserInfo()).countuserbyname(username);
        }


        /// <summary>
        /// 计算指定用户的好友数量
        /// </summary>
        /// <param name="hostID">用户注册邮箱</param>
        /// <returns>用户好友数量</returns>
        public int countfriendbyID(String hostID)
        {
            return (new b_Friends()).countfriendbyID(hostID);
        }



        /// <summary>
        /// 增加两个好友之间的亲密度
        /// </summary>
        /// <param name="hostID">主用户注册邮箱</param>
        /// <param name="guestID">副用户注册邮箱</param>
        /// <returns>修改是否成功</returns>
        public bool incFriendDeg(String hostID, String guestID)
        {
            return (new b_Friends()).incfrienddeg(hostID, guestID);
        }

        /// <summary>
        /// 获得两个好友之间的亲密度
        /// </summary>
        /// <param name="hostID">主用户注册邮箱</param>
        /// <param name="guestID">副用户注册邮箱</param>
        /// <returns>好友亲密度</returns>
        public int getFriendDeg(String hostID, String guestID)
        {
            return (new b_Friends()).getfrienddeg(hostID, guestID);
        }


        public void SetFriendPhoto()
        {

        }

        public void ShowRecentActivity()
        {

        }
    }
}
